package report

import (
	"../common"
	"../web"
	"strings"
)

type SearchObj struct {
	common.BaseHandler
}

func (self *SearchObj) ParameterCheck() bool {
	s_search := strings.TrimSpace(self.Ctx.Params["q"])
	if s_search == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_search)

	return true
}

func (self *SearchObj) SqlQueryExecute() {
	ssns := *common.FetchOneRowPtr(
		`SELECT TOP 1 SSN_NO
           FROM SFC_SSN(NOLOCK)
          WHERE SSN_NO=?`,
		self.Usr.Conn, new(common.RecDataStr), self.Sqlparas...)

	custid := *common.FetchOneRowPtr(
		`SELECT TOP 1 SSN_NO
           FROM SFC_SSN(NOLOCK)
          WHERE CUST_PRODNO=?`,
		self.Usr.Conn, new(common.RecDataStr), self.Sqlparas...)

	wos := *common.FetchOneRowPtr(
		`SELECT TOP 1 WONO
           FROM SFC_WO(NOLOCK)
          WHERE WONO=?`,
		self.Usr.Conn, new(common.RecDataStr), self.Sqlparas...)

	items := *common.FetchOneRowPtr(
		`SELECT TOP 1 ITEM
           FROM SFC_ITEMS(NOLOCK)
          WHERE ITEM=?`,
		self.Usr.Conn, new(common.RecDataStr), self.Sqlparas...)

	boxs := *common.FetchOneRowPtr(
		`SELECT TOP 1 BOXID
           FROM SFC_BOX(NOLOCK)
          WHERE BOXID=?`,
		self.Usr.Conn, new(common.RecDataStr), self.Sqlparas...)

	pos := *common.FetchOneRowPtr(
		`SELECT TOP 1 PONO
           FROM SFC_DELIVERY(NOLOCK)
          WHERE PONO=?`,
		self.Usr.Conn, new(common.RecDataStr), self.Sqlparas...)

	routs := *common.FetchOneRowPtr(
		`SELECT TOP 1 NAME
           FROM SFC_ROUTING(NOLOCK)
          WHERE NAME=?`,
		self.Usr.Conn, new(common.RecDataStr), self.Sqlparas...)

	self.TmplData["ssns"] = ssns
	self.TmplData["custid"] = self.Sqlparas[0]
	self.TmplData["custlinked"] = custid
	self.TmplData["wos"] = wos
	self.TmplData["items"] = items
	self.TmplData["boxs"] = boxs
	self.TmplData["pos"] = pos
	self.TmplData["routs"] = routs
}

func SearchObjHandler(ctx *web.Context) {
	self := new(SearchObj)
	self.Ctx = ctx
	self.Html = "search.html"
	self.DoQueryUrl(self)
}
